package com.example.oct.controller;

import com.example.oct.pojo.Questionnaire;
import com.example.oct.service.QuestionnaireService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "问卷系统")
@Controller
public class QuestionnaireController {

    @Autowired
    private QuestionnaireService questionnaireService;

    @Operation(summary = "查询所在页面的图表")
    @RequestMapping("/echarts/{eid}")
    public String echarts1(@PathVariable("eid") String eid) {
        return "echarts/echarts" + eid;
    }

    @Operation(summary = "查询所在页面的问卷")
    @RequestMapping("/ques/{sheetName}")
    public String queryQues(@PathVariable("sheetName") String sheetName, Model model) {
        List<Questionnaire> questionnaires = questionnaireService.queryQues(sheetName);
        model.addAttribute("questionnaires", questionnaires);
        model.addAttribute("sheetName", sheetName);
        return "ques/" + sheetName;
    }

    @Operation(summary = "跳转至修改内容页面")
    @GetMapping("/change/{pageName}")
    public String toChange(@PathVariable("pageName") String pageName) {
        return "ques/" + pageName;
    }

    @Operation(summary = "导入问卷")
    @PostMapping("/addQues")
    @ResponseBody
    public String addQues(String content, String sheetName) {
        questionnaireService.addQues(content, sheetName);
        return "success";
    }

    @Operation(summary = "发布问卷")
    @PostMapping("/impQues")
    @ResponseBody
    public String impQues(String sheetName) {
        boolean isSuccess = questionnaireService.importQues(sheetName);
        return isSuccess ? "success" : "fail";     //返回
    }

    @Operation(summary = "导出问卷")
    @RequestMapping("/expQues")
    public void expQues(@RequestParam("sheetName") String sheetName, HttpServletResponse response) {
        questionnaireService.exportQues(sheetName, response);
    }

    @Operation(summary = "修改问卷")
    @PostMapping("/updateQues")
    @ResponseBody
    public String updateQues(@RequestBody String json) {
        boolean isSuccess = questionnaireService.updateQues(json);
        return isSuccess ? "success" : "fail";     //返回
    }

    @Operation(summary = "删除问卷")
    @RequestMapping("/delQues")
    @ResponseBody
    public String delQues(int id, String sheetName) {
        boolean isSuccess = questionnaireService.deleteQues(id, sheetName);
        return isSuccess ? "success" : "fail";     //返回
    }

}
